Visual Basic 2003: Code  

Code: Deleting a Registry Key (Visual Basic)

The following example creates a registry key and two subkeys. It retrieves the first key's value and then retrieves the values of the first key as well as its subkeys. It then deletes the second subkey and prints the values of the first key and its subkeys to confirm the deletion of the second subkey.

Example

Private Sub CreateAndDeleteKey
    SaveSetting("TestApp","FirstKey","FirstSubKey","FirstSub")
    SaveSetting("TestApp","FirstKey","SecondSubKey","SecondSub")
    Dim firstKey As String = GetSetting("TestApp", "FirstKey", _
        "FirstSubKey")
    Dim allSettings(,) As String = GetAllSettings("TestApp", "FirstKey")
    DeleteSetting("TestApp", "FirstKey", "SecondSubKey")
    firstKey = GetSetting("TestApp", "FirstKey", "SecondSubKey")
    allSettings = GetAllSettings("TestApp", "Startup")
End Sub
Note   The second call to GetSetting returns an empty string, because the subkey has been deleted. The first call to GetAllSettings returns two key/value pairs, the second call returns only one key/value pair.

Robust Programming

The GetSetting method returns an empty string if the key/value pair does not exist.

Security

Registry calls fail if either sufficient runtime permissions are not granted (RegistryPermission Class) or if the user does not have the correct ACLs for creating or writing to settings. For example, a local application that has the code access security permission might not have operating system permission.

See Also

Code: Adding Data to the Registry | Registry Access Changes in Visual Basic .NET | Registry Class | Registry Summary | GetSetting | GetAllSettings | System Input/Output Example Topics